package com.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import exception.DAOSystemException;

import model.UserDTO;

import bizlogic.UserManager;

/**
 * @Pranjal
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}
	private Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
				doForward(request, response);
	}

	private void doForward(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        RequestDispatcher rd = null;
		Object o = request.getSession().getAttribute("user");
		if(o!=null)
		{
			UserDTO user =(UserDTO)o;
			if(user.getPosition().getRole().equalsIgnoreCase("Admin"))
			{
            	// go to admin page
              	rd = context.getRequestDispatcher("/admin.jsp");
              	rd.include(request, response);
            	//out.println("ADMIN!!!");
			}
			else if(!user.getPosition().getRole().equalsIgnoreCase("Admin")){
                // go to staff page
            	rd = context.getRequestDispatcher("/staff.jsp");
            	rd.include(request, response);
            	//out.println("STAFF!!!");
			}
		}
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ServletContext context = getServletContext();
        RequestDispatcher rd = null;
        PrintWriter out = response.getWriter();
        UserManager userManager = new UserManager();
        String userName = request.getParameter("userName");
        //System.out.println(userName);
        String password =  request.getParameter("password");
        //System.out.println(password);
        String role = request.getParameter("role");
        //System.out.println(role);
        UserDTO user=null;
		try {
			user = userManager.findUser(userName);
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        if(user!=null){
            if(user.getPassword().equals(password)){
                    if(role.equalsIgnoreCase("admin") && user.getPosition().getRole().equalsIgnoreCase("Admin")){
                       
                    	HttpSession session = request.getSession(false);
                        session.setAttribute("user", user);
                        doForward(request, response);
                    }
                    else if(!role.equalsIgnoreCase("admin") && !user.getPosition().getRole().equalsIgnoreCase("Admin")){                    	
                    	HttpSession session = request.getSession(false);
                        session.setAttribute("user", user);
                        doForward(request, response);;
                    }
                    else
                    {
                        // go to staff page
                    	rd = context.getRequestDispatcher("/welcome.jsp");
                    	rd.include(request, response);
                    }
            	}
           
            else{
                if(role.equalsIgnoreCase("admin")){
                    
                	// go to admin page
                  	rd = context.getRequestDispatcher("/AdminLogin.jsp");
                  	request.setAttribute("msg", "TRY AGAIN!!!");
                  	rd.include(request, response);
                }
                else if(!role.equalsIgnoreCase("admin")){                    	
                	// go to admin page
                  	rd = context.getRequestDispatcher("/StaffLogin.jsp");
                	request.setAttribute("msg", "TRY AGAIN!!!");
                  	rd.include(request, response);
                }
                else
                {
                    // go to staff page
                	rd = context.getRequestDispatcher("/welcome.jsp");
                	rd.include(request, response);
                }
            }
        }
        else{
           
            if(role.equalsIgnoreCase("admin")){
                
            	// go to admin page
              	rd = context.getRequestDispatcher("/AdminLogin.jsp");
              	request.setAttribute("msg", "TRY AGAIN!!!");
              	rd.include(request, response);
            }
            else if(!role.equalsIgnoreCase("admin")){                    	
            	// go to admin page
              	rd = context.getRequestDispatcher("/StaffLogin.jsp");
            	request.setAttribute("msg", "TRY AGAIN!!!");
              	rd.include(request, response);
            }
            else
            {
                // go to staff page
            	rd = context.getRequestDispatcher("/welcome.jsp");
            	rd.include(request, response);
            }
        }
       
        }
	
	
}
